package com.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @GetMapping("/flowLimit")
    @SentinelResource(value = "flowLimit",blockHandler = "blockHandler")
    public Object flowLimit(){
        return "flowLimit";
    }
    public Object blockHandler(BlockException blockException){
        return "访问次数过于频繁";
    }

    @GetMapping("/fallback")
    @SentinelResource(value = "fallback",fallback = "fallbackHandler")
    public Object fallback(){
        int i=10/0;
        return "fallback";
    }
    public Object fallbackHandler(){
        return "出异常了回退过来。。。";
    }

    @GetMapping("/exception")
    @SentinelResource(value = "exception",exceptionsToIgnore ={NullPointerException.class, FlowException.class})
    public Object exception(String str){
        if(StringUtils.isEmpty(str)){
           throw new NullPointerException();
        }
        return "fallback";
    }
}
